package com.example.aslongastoday.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.aslongastoday.databinding.ViewRecycleBacklogBinding;
import com.example.aslongastoday.db.dao.BackLogDao;
import com.example.aslongastoday.domain.BackLog;

import java.util.List;

public class RecycleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<BackLog> backLogList;
    private BackLogDao backLogDao;
    private RecycleAdapter recycleAdapter;

    public RecycleAdapter(Context context, List<BackLog> backLogList) {
        this.context = context;
        this.backLogList = backLogList;
        backLogDao = BackLogDao.getInstance(context);
        recycleAdapter = this;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(ViewRecycleBacklogBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder1, int position) {
        ViewHolder holder = (ViewHolder) holder1;

        holder.backlogItemContext.setText(backLogList.get(position).getContent());

        int i = position;
        BackLog backLog = backLogList.get(position);
        //删除按钮监听
        holder.iv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                backLogDao.delete(backLog.getGroupId(),backLog.getPosition());
                backLogList.remove(i);
                recycleAdapter.notifyDataSetChanged();
                Toast.makeText(context, "永久删除", Toast.LENGTH_SHORT).show();
            }
        });

        //还原按钮监听
        holder.iv_refresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                backLog.setStatus(0);
                backLogDao.update(backLog.getGroupId(),backLog.getPosition(),backLog);
                backLogList.remove(i);
                recycleAdapter.notifyDataSetChanged();
                Toast.makeText(context, "还原待办", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return backLogList==null?0:backLogList.size();
    }

    private class ViewHolder extends RecyclerView.ViewHolder{
        public final ImageView iv_refresh;
        public final TextView backlogItemContext;
        public final ImageView iv_delete;

        public ViewHolder(ViewRecycleBacklogBinding binding) {
            super(binding.getRoot());
            iv_refresh = binding.ivRefresh;
            backlogItemContext = binding.backlogItemContext;
            iv_delete = binding.ivDelete;
        }
    }

}
